<template>
  <el-container class="home-container">
<!--    头部区域-->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button @click="exit" type="info">
        退出
      </el-button>
    </el-header>
<!--    主体区域-->
    <el-container>
<!--      左侧边栏-->
      <el-aside :width="collapse ? '60px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu :unique-opened="true" :collapse="collapse" :default-active="activePath" :router="true">
          <el-submenu  v-for="(item, index) in menuList" :key="item.id" :index="index + ''">
<!--            一级菜单-->
            <template slot="title">
<!--              图标-->
              <i class="el-icon-location"></i>
<!--              文字-->
              <span>{{item.authName}}</span>
            </template>
<!--            二级菜单-->
            <el-menu-item @click="saveNavState('/' + subitem.path)"   :index="'/' + subitem.path" v-for="subitem in item.children"  style="border: none" :key="subitem.id + ''">
              <template slot="title">
                <!--              图标-->
                <i class="el-icon-menu"></i>
                <!--              文字-->
                <span>{{subitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>

        </el-menu>
      </el-aside>
<!--      右边内容-->
      <el-main>
<!--        路由占位符-->
        <router-view>

        </router-view>

      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      menuList: [],
      collapse: false,
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 获取所有菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    exit () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },

    // 点击按钮切换展开折叠
    toggleCollapse () {
      this.collapse = !this.collapse
    },

    // 保存链接激活状态
    saveNavState (activ) {
      window.sessionStorage.setItem('activePath', activ)
      this.activePath = activ
    }
  }
}
</script>

<style scoped>

  .home-container{
    height: 100%;
  }
  .el-header, .el-footer {
    background-color: #B3C0D1;
    color: #333;
    text-align: center;
    line-height: 60px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .el-header div{
    display: flex;
    align-items: center;
  }

  .el-header div img{
    height: 40px;
    width: 40px;
  }

  .el-aside {
    background-color: #D3DCE6;
    color: #333;

  }

  .el-main {
    background-color: #E9EEF3;
    color: #333;
  }

  body > .el-container {
    margin-bottom: 40px;
  }

  .toggle-button{
    background-color: #2c3e50;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    color: white;
    letter-spacing: .2em;
    cursor: pointer;
  }
</style>
